package com.smart.business.controller;

import com.smart.aop.log.LogType;
import com.smart.aop.log.SaveLog;
import com.smart.aop.permission.HasPermission;
import com.smart.business.dao.MeterTypeDao;
import com.smart.business.dao.PointDao;
import com.smart.common.utils.DateUtil;
import com.smart.entity.business.ParamEntity;
import com.smart.entity.business.ParameterTemplateEntity;
import com.smart.entity.business.PointEntity;
import com.smart.model.excel.annotation.ExcelField;
import com.smart.model.response.r.Result;
import com.smart.model.response.r.ResultCode;
import com.smart.model.response.r.ResultWrapper;
import com.smart.service.business.ParameterTemplateService;
import com.smart.tools.excel.ExcelExport;
import com.smart.tools.excel.ExcelImport;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 参数模板 Controller
 *
 * @author wf
 * @since 2025-07-15 13:03:48
 */
@RestController
@RequestMapping("/business/parameterTemplate")
public class ParameterTemplateController {
    @Autowired
    private ParameterTemplateService parameterTemplateService;
    @Autowired
    private ResultWrapper resultWrapper;
    @Autowired
    private MeterTypeDao meterTypeDao;
    @Autowired
    private PointDao pointDao;

    /**
     * 列表
     *
     * @param parameterTemplateEntity 参数模板bean
     * @return String
     */
    @GetMapping("/page")
    public String page(ParameterTemplateEntity parameterTemplateEntity) {
        //转化meterTypeId
        List<ParameterTemplateEntity> list = parameterTemplateService.findPage(parameterTemplateEntity);
        list.forEach(point -> {
            point.setMeterTypeId(meterTypeDao.selectById(point.getMeterTypeId()).getName());
        });
        return Result.data(list);
    }

    /**
     * 列表（不分页）
     *
     * @param parameterTemplateEntity 参数模板bean
     * @return String
     */
    @GetMapping("/list")
    public String list(ParameterTemplateEntity parameterTemplateEntity) {

        return Result.data(parameterTemplateService.findList(parameterTemplateEntity));
    }

    /**
     * 信息
     *
     * @param id 主键ID
     * @return String
     */
    @GetMapping("/get")
    public String get(@RequestParam String id) {
        ParameterTemplateEntity result = parameterTemplateService.get(id);
        return result != null ? Result.data(result) : Result.fail(ResultCode.FAIL);
    }

    /**
     * 保存
     *
     * @param parameterTemplateEntity 参数模板bean
     * @return String
     */
    @HasPermission("parameterTemplate:add")
    @PostMapping("/save")
    @SaveLog(module = "参数模板管理" , type = LogType.ADD)
    public String save(@RequestBody ParameterTemplateEntity parameterTemplateEntity) {
        String MeterTypeId = meterTypeDao.selectByName(parameterTemplateEntity.getMeterTypeId());
        if (MeterTypeId == null){
            return Result.fail("能源类型不存在");
        }
        parameterTemplateEntity.setMeterTypeId(MeterTypeId);
        ParameterTemplateEntity result = parameterTemplateService.saveEntity(parameterTemplateEntity);
        return result != null ? Result.data(result) : Result.fail(ResultCode.FAIL);
    }

    /**
     * 修改
     *
     * @param parameterTemplateEntity 参数模板bean
     * @return String
     */
    @HasPermission("parameterTemplate:update")
    @PostMapping("/update")
    @SaveLog(module = "参数模板管理" , type = LogType.UPDATE)
    public String update(@RequestBody ParameterTemplateEntity parameterTemplateEntity) {
        ParameterTemplateEntity result = parameterTemplateService.updateEntity(parameterTemplateEntity);
        return result != null ? Result.data(result) : Result.fail(ResultCode.FAIL);
    }

    /**
     * 删除
     *
     * @param parameterTemplateEntity 参数模板bean
     * @return String
     */
    @HasPermission("parameterTemplate:delete")
    @PostMapping("/delete")
    @SaveLog(module = "参数模板管理" , type = LogType.DELETE)
    public String delete(@RequestBody ParameterTemplateEntity parameterTemplateEntity) {
        return Result.status(parameterTemplateService.delete(parameterTemplateEntity));
    }

    /**
     * 导出
     *
     * @param parameterTemplateEntity 参数模板bean
     * @param response 响应
     */
    @HasPermission("parameterTemplate:export")
    @PostMapping(value = "/export")
    public void exportData(@RequestBody ParameterTemplateEntity parameterTemplateEntity, HttpServletResponse response) {
        List<ParameterTemplateEntity> list = resultWrapper.setListIdentityInfo(parameterTemplateService.findList(parameterTemplateEntity), ParameterTemplateEntity.class);
        String fileName = "参数模板数据" + DateUtil.getDate("yyyyMMddHHmmss") + ".xlsx";
        try (ExcelExport export = new ExcelExport("参数模板数据", ParameterTemplateEntity.class)) {
            export.setDataList(list).write(response, fileName);
        }
    }

    /**
     * 导入
     *
     * @param request 请求
     */
    @HasPermission("parameterTemplate:import")
    @PostMapping(value = "/import")
    public String importData(MultipartHttpServletRequest request) {
        MultiValueMap<String, MultipartFile> multiFileMap = request.getMultiFileMap();
        List<MultipartFile> files = multiFileMap.get("files");
        try (ExcelImport excelImport = new ExcelImport(files.get(0), 2, 0)) {
            List<ParameterTemplateEntity> dataList = excelImport.getDataList(ParameterTemplateEntity.class);
            return Result.status(parameterTemplateService.saveBatch(dataList));
        } catch (Exception e) {
            return Result.fail("导入失败！");
        }
    }

    /**
     * 下载导入模板
     *
     * @param response 响应
     */
    @GetMapping(value = "/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response) {
        String fileName = "导入参数模板模板.xlsx";
        try (ExcelExport export = new ExcelExport("参数模板数据", ParameterTemplateEntity.class, null, ExcelField.Type.IMPORT)) {
            export.write(response, fileName);
        }
    }
    /**
     * 模板参数定义
     * @param templateId 模板参数定义
     */
    @GetMapping("/templateParam")
    public String templateParam(@RequestParam String templateId){
        return Result.data(parameterTemplateService.templateParam(templateId));
    }
    /**
     * 添加参数
     * @param paramEntity 参数模板bean
     */
    @PostMapping("/addParam")
    public String addParam(@RequestBody ParamEntity paramEntity){
      return Result.status(parameterTemplateService.addParam(paramEntity));
    }
    /**
     * 删除参数
     * @param paramEntity 参数id
     */
    @PostMapping("/deleteParam")
    public String deleteParam(@RequestBody ParamEntity paramEntity){
        return Result.status(parameterTemplateService.deleteParam(paramEntity.getId()));
    }
}
